C     Program EX_0804.FOR
C     Listing 16F - see documentation in TUTOR.SSS
 
$include:'SSSF1.H'
 
      subroutine prime
$include:'SSSF2.H'
      TIMEL = 150
      call INIQUE(1, 0, 1)
      call CREATE(0, n)
      call SIMEND(TIMEL)
      open (7, FILE='SSS.PRN', MODE='WRITE')
      return
      end
 
      Program EX_0804
$include:'SSSF2.H'
      integer ARRIVL, STARTA, ENDACT, NEXTAC,
     +  n, server, ecode
      data  ARRIVL/1/, STARTA/2/, ENDACT/3/, NEXTAC/4/
      data  n/0/, server/1/
 
      call prime
  200 format(A, F7.2, A, I3, A, F4.1, A, F5.2)
  201 format(A, F7.2, 1X, I3, 1X, F4.1, 1X, F5.2)
 
   99 ecode = NEXTEV()
      if (ecode.gt.0) then
        goto (101, 102, 103, 104) ecode
 
C ARRIVL
  101   continue
        n = n + 1
        call CREATE(EX(1), n)
        call SCHED(0, NEXTAC, IDE())
        goto 99
 
C NEXTAC
  104   continue
        if (server.gt.0) then
          call SCHED(0.0, STARTA, IDE())
        else
          call QUEUE(1, 0.0)
          write(7, 201)' ',T(),NQ(1),QAVG(1),QSTD(1)
          write(*, 200)' At ',T(),' q size:',NQ(1),
     +             ' ave q:',QAVG(1),' std:',QSTD(1)
        endif
        goto 99
 
C STARTA
  102   continue
        call SCHED(EX(0.9), ENDACT, IDE())
        server = server - 1
        goto 99
 
C ENDACT
  103   continue
        call DISPOS
        server = server + 1
        if (NQ(1).gt.0) then
          call REMVFQ(1, 1)
          call SCHED(0.0, STARTA, IDE())
          write(7, 201)' ',T(),NQ(1),QAVG(1),QSTD(1)
          write(*, 200)' At ',T(),' q size:',NQ(1),
     +             ' ave q:',QAVG(1),' std:',QSTD(1)
        endif
        goto 99
 
      else
        stop 'End of simulation'
      endif
      end
 